package com.ocrgroup.sample.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.Target;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;

public class OcrResultImgAdapter extends PagerAdapter {

    private Context mContext;
    private ArrayList<Bitmap> list;

    public OcrResultImgAdapter(Context context, ArrayList<Bitmap> pathList) {
        this.list = pathList;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    //判断界面是否应该显示
    //参1：界面的View对象
    //参2：显示界面的View对象
    @Override
    public boolean isViewFromObject(View view, Object object) {
        // TODO Auto-generated method stub
        return view == object;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    //添加条目的方法
    @NonNull
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        PhotoView imageView = new PhotoView(mContext);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(400, 600));
//        imageView.setImageDrawable(list.get(position));
        // 解析base64图片数据并设置
        Glide.with(mContext)
                .load(list.get(position))
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                .into(imageView);
        container.addView(imageView);
        return imageView;
    }

    //删除条目的方法
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
//			super.destroyItem(container, position, object);
        container.removeView((View) object);
    }

}
